package com.xiaoyi.intentsdklibrary.Bean;

import android.content.Context;
import com.xiaoyi.intentsdklibrary.Bean.AppBeanDao;
import com.xiaoyi.intentsdklibrary.Bean.DaoMaster;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;

public class AppBeanSqlUtil {
    private static final AppBeanSqlUtil ourInstance = new AppBeanSqlUtil();
    private AppBeanDao mAppBeanDao;

    public static AppBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private AppBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mAppBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "intentsdk-db", null).getWritableDatabase()).newSession().getAppBeanDao();
    }

    public void add(AppBean appBean) {
        this.mAppBeanDao.insertOrReplace(appBean);
    }

    public void addList(List<AppBean> list) {
        this.mAppBeanDao.insertOrReplaceInTx(list);
    }

    public void update(AppBean appBean) {
        this.mAppBeanDao.update(appBean);
    }

    public void delByID(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mAppBeanDao.queryBuilder().where(AppBeanDao.Properties.PackageName.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mAppBeanDao.deleteInTx(arrayList);
        }
    }

    public void delAll() {
        try {
            this.mAppBeanDao.deleteInTx(this.mAppBeanDao.queryBuilder().build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<AppBean> searchByID(String str) {
        Exception e;
        ArrayList arrayList = new ArrayList();
        if (str == null) {
            str = "";
        }
        try {
            ArrayList arrayList2 = (ArrayList) this.mAppBeanDao.queryBuilder().where(AppBeanDao.Properties.PackageName.eq(str), new WhereCondition[0]).list();
            if (arrayList2 != null) {
                return arrayList2;
            }
            try {
                return new ArrayList();
            } catch (Exception e2) {
                arrayList = arrayList2;
                e = e2;
                try {
                    e.printStackTrace();
                    return arrayList;
                } catch (Throwable unused) {
                    return arrayList;
                }
            } catch (Throwable unused2) {
                return arrayList2;
            }
        } catch (Exception e3) {
            e = e3;
            e.printStackTrace();
            return arrayList;
        }
    }

    public List<AppBean> searchAll() {
        List<AppBean> list = this.mAppBeanDao.queryBuilder().build().list();
        return list == null ? new ArrayList() : list;
    }
}
